# Make LaTeX chunk for Table 1

# Load Models and Store AUCs
countries <- c('indo', 'colo')
dvs <- c("any", "high", "spike")
models <- c('full', 
            "ols",
            "lagdv",
            "fe",
            "aggfe")

model.obj.names <- c(full="ebma.results",
                     lagdv="fe.results",
                     ols="fe.results",
                     fe="fe.results",
                     aggfe="fe.results")

auc_array <- array(NA,
                   dim = c(length(countries),
                           length(dvs),
                           length(models)),
                   dimnames = list(countries,
                                   dvs,
                                   models))
algo <- "ebma"

for (country in countries) {
  for (model in models) {
    for (dv in dvs) {
           filename <- paste(modeldir,"/",
                    country,
                    "_",
                    ifelse(model=="full","ebma", model),
                    "_",
                    dv,
                    ifelse(model=="full","_full", ""),
                    ".RData",
                    sep = "")
      load(filename)
      aucs_by_year = c()
      for (i in 1:length(get(model.obj.names[model]))) {
          predictions <- as.vector(get(model.obj.names[model])[[i]]$fit.oos)
          realizations <- as.vector(get(model.obj.names[model])[[i]]$actual.oos)
          aucs_by_year <- c(aucs_by_year,
                            roc(response = realizations,
                                predictor = predictions)[['auc']])
      }
      auc_array[country, dv, model] <- mean(aucs_by_year)
    }
  }
}


# Build Table with Labels
country.labels <- c(indo='Indonesia (social conflict, 2008-2014)',
                    colo='Colombia (attacks and clashes, 1998-2005)')
outcome.labels <- c(any="Any violent event",
                    high="$\\ge$ 5 violent events",
                    spike="$\\ge$ 1 s.d. increase in events")
model.labels <- c(full="Baseline\\\\ EBMA",
                  lagdv="Lagged\\\\ Predictand",
                  ols="OLS",
                  fe="Location\\\\ FE Only",
                  aggfe="Department/\\\\ District FE \\\\ Only")

table <- auc_array
dimnames(table)[[1]] <- country.labels[dimnames(table)[[1]]]
dimnames(table)[[2]] <- outcome.labels[dimnames(table)[[2]]]
dimnames(table)[[3]] <- model.labels[dimnames(table)[[3]]]

# Print to Latex
print.texfragment(table = table,
                  filepath = "tables",
                  file = "table_2",
                  head.width = '2')

